package com.travelsmart.server.integration;

import java.util.List;

import com.smart.hotelsmart.BillingAddressType;
import com.smart.hotelsmart.BookHotelRequestType;
import com.smart.hotelsmart.BookRoomType;
import com.smart.hotelsmart.ListBookRoomType;
import com.smart.hotelsmart.PaymentType;
import com.travelsmart.client.bean.to.ReservaHotelTO;
import com.travelsmart.client.bean.to.ReservaQuartoTO;
import com.travelsmart.server.util.UtilDate;

public class BuilderBookHotelRequestType {
	
	
	public BookHotelRequestType criarListBookRoom(ReservaHotelTO reserva, String username) {
		assert reserva != null;
		BookHotelRequestType request = new BookHotelRequestType();
		request.setArrival(UtilDate.criarGregorianCalendar(reserva.getDataEntrada()));
		request.setCustomer(username);
		request.setBillingAddress(criarBillingAddressType(reserva));
		request.setCodeCustomer(reserva.getCodigoCliente());
		request.setCodeHotel(reserva.getHotel().getCodigo());
		request.setDeparture(UtilDate.criarGregorianCalendar(reserva.getDataSaida()));
		request.setListBookRoom(criarListBookRoom(reserva));
		request.setPayment(criarPaymentType(reserva));
		return request;
	}
	
	
	private ListBookRoomType criarListBookRoom(ReservaHotelTO reserva){
		assert reserva.getQuartos() != null;
		ListBookRoomType listBookRoomType = new ListBookRoomType();
		List<ReservaQuartoTO> quartos = reserva.getQuartos();
		for (ReservaQuartoTO reservaQuartoTO : quartos) {
			BookRoomType bookRoomType = new BookRoomType();
			listBookRoomType.getBookRoom().add(bookRoomType);
			bookRoomType.setAmountAdults(reservaQuartoTO.getQtiAdultos());
			bookRoomType.setAmountChildren(reservaQuartoTO.getQtiCrianca());
			assert reservaQuartoTO.getQuarto() != null;
			assert reservaQuartoTO.getQuarto().getCodigoQuarto() != 0;
			bookRoomType.setCodeRoom(reservaQuartoTO.getQuarto().getCodigoQuarto());
			bookRoomType.setFirstNameHost(reservaQuartoTO.getNome());
			bookRoomType.setLastNameHost(reservaQuartoTO.getSobrenome());
		}
		return listBookRoomType;
	}
	
	private BillingAddressType criarBillingAddressType(ReservaHotelTO reserva){
		assert reserva.getEnderecoPagamento() != null;
		BillingAddressType billingAddress = new BillingAddressType();
		billingAddress.setCity(reserva.getEnderecoPagamento().getCidade());
		billingAddress.setCountry(reserva.getEnderecoPagamento().getPais());
		billingAddress.setState(reserva.getEnderecoPagamento().getEstado());
		billingAddress.setStreetAddress(reserva.getEnderecoPagamento().getEndereco());
		billingAddress.setZipCode(reserva.getEnderecoPagamento().getCep());
		return billingAddress;
		
	}
		
	private PaymentType criarPaymentType(ReservaHotelTO reserva){
		PaymentType payment = new PaymentType();
		payment.setTotal(reserva.custo());
		//# INICIO - EXERC. 6
		assert reserva.getCartao() != null;
		payment.setCardNumber(reserva.getCartao().getNumero());
		payment.setCardType(reserva.getCartao().getBandeira().getValor());
		payment.setExpiryMonth(reserva.getCartao().getMesVencimento());
		payment.setExpiryYear(reserva.getCartao().getAnoVencimento());
		payment.setFirstName(reserva.getCartao().getNomeProprietario());
		payment.setLastName(reserva.getCartao().getSobrenomeProprietario());
		payment.setSecurityCode(reserva.getCartao().getCodigoSeguranca());
		//FIM - EXERC. 6 #/
		return payment;

	}
	

}
